package com.globallogic.javatrainings.sergiiivanov.model;

import java.io.Serializable;

import com.globallogic.javatrainings.sergiiivanov.resources.ErrorCodes;

/**
 * Keeps collection of teachers and provides basic collection management
 * 
 * @author Sergey
 * 
 */
public class TeachersManager implements Serializable, NameUniqueObserver {
    private static final long serialVersionUID = 1L;
    private MyArray<Teacher> teachers;

    /**
     * Default constructor. Used in University
     */
    public TeachersManager() {
        teachers = new MyArray<Teacher>(new Teacher[0]);
    }

    /**
     * Creates new teacher record and adds it into collection<br>
     * Validates name before adding
     * 
     * @param name
     *            - valid name; not null, not empty, unique in scope of
     *            collection
     * @param mobile
     *            - free string
     * @param email
     *            - free string
     * @throws UniversityWarning
     *             name is not valid
     */
    public void addPerson(String name, String mobile, String email) throws UniversityWarning {
        Teacher newTeacher = new Teacher();
        newTeacher.addObserver(this);
        newTeacher.setName(name);
        newTeacher.setMobile(mobile);
        newTeacher.setEmail(email);
        teachers.add(newTeacher);
    }

    /**
     * @return number of teachers in the collection
     */
    public int countTeachers() {
        return teachers.size();
    }

    /**
     * @return copy of collection as {@link MyArray} of {@link Teacher}
     */
    public MyArray<Teacher> getTeachers() {
        return teachers.copy();
    }

    /**
     * Removes teacher from collection
     * 
     * @param t
     *            {@link Teacher} to remove
     * @throws UniversityFailure
     *             code is broken in MyArray.remove(T o)
     */
    public void removeTeacher(Teacher t) {
        t.removeLinks();
        teachers.remove(t);
    }

    /**
     * Returns teacher stored under index
     * 
     * @param index
     *            valid index (0<= index < {@link #countTeachers()})
     * @return {@link Teacher}
     * @throws UniversityWarning
     *             invalid index
     * @throws UniversityFailure
     *             code is broken
     */
    public Teacher getTeacherByIndex(int index) throws UniversityWarning, UniversityFailure {
        if (index < 0 || countTeachers() <= index) {
            throw new UniversityWarning(ErrorCodes.ARRAY_INDEX_IS_OUT_OF_BOUNDARIES);
        }
        return teachers.get(index);
    }

    /**
     * Throws UniversityWarning if name is found in collection of teachers<br>
     * Do not use it against name from collection - will find itself
     * 
     * @param name
     *            - name to lookup
     * @throws UniversityWarning
     *             name is found in collection
     */
    @Override
    public void hasDuplicates(String name) throws UniversityWarning {
        if (name != null) {
            for (MyArrayToNamesCovertable existingOne : teachers) {
                if (name.equals(existingOne.getName()))
                    throw new UniversityWarning(ErrorCodes.PERSON_NAME_NOT_UNIQUE + name);
            }
        }
    }
}
